package com.mobi.Survey;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.Toast;

import com.mobi.Survey.controller.User.Image;
import com.mobi.Survey.utils.Device;

/**
 * 
 * <p>
 * <b>Author:</b> Yahya Arshad
 * <p>
 * <b>Company:</b> Mobi Media Soft
 * <p>
 * <b> Website:</b> www.mobimediasoft.com
 * <p>
 * AbstractScreen.java is parent class for all the activities and contains most
 * generic methods used by child activities
 * 
 */
public abstract class AbstractScreen extends Activity
{

	protected static boolean ADS_ENABLE = true;

	protected View headerImage;
	/**
	 * if true non title will not be render
	 */
	protected boolean hideTitle;
	/**
	 * When EditText is first focus of an activity keyboard is automatically
	 * open with activity launch if child activity assign it true keyboard will
	 * not be shown automatically
	 */
	protected boolean hideKeaboard;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{

		super.onCreate(savedInstanceState);
		ADS_ENABLE = getResources().getBoolean(R.bool.ADS_ENABLE);
		if (hideTitle)
		{
			hideDefalutTitle();
		}

		initLayoutIds();
		int id = getLayoutId();
		if (id > 0)
			setContentView(id);
		initUiVars();

		getWindow().setSoftInputMode(
				WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

	}

	/**
	 * 
	 * @param dialNo
	 *            Number to call within application
	 */
	public void makeCall(String dialNo)
	{

		if (dialNo == null || dialNo.length() == 0)
			return;

		Intent callIntent = new Intent(Intent.ACTION_CALL);

		callIntent.setData(Uri.parse("tel:" + dialNo));
		startActivity(callIntent);
	}

	/**
	 * 
	 * @param email
	 *            To Email Address
	 * @param subject
	 *            Subject of Email Address
	 */
	public void sendEmail(String email, String subject)
	{
		if (email == null || email.length() == 0)
			return;
		final Intent emailIntent = new Intent(
				android.content.Intent.ACTION_SEND);

		emailIntent.setType("message/rfc822");

		emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
				new String[] { email });
		emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
		emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, getResources()
				.getString(R.string.more_info));
		startActivity(Intent.createChooser(emailIntent, "Send Email..."));
	}

	/**
	 * 
	 * @param url
	 *            Url of website to open in Android default browser
	 */
	public void openUrl(String url)
	{
		if (url == null || url.length() == 0)
			return;
		if (!url.startsWith("http://"))
		{
			url = "http://" + url.trim();
		}
		Intent i = new Intent(Intent.ACTION_VIEW);
		i.setData(Uri.parse(url));
		startActivity(i);
	}

	public void setContentView(int resourceId)
	{
		super.setContentView(resourceId);
	}

	protected void showLongToast(String msg)
	{
		Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
	}

	protected void showShortToastOnUiThread(final Context context,
			final String msg)
	{
		runOnUiThread(new Runnable()
		{

			@Override
			public void run()
			{
				Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();

			}
		});
	}

	/**
	 * 
	 * @param message
	 *            Shows a short Toast with message
	 */
	protected void showShortToast(String message)
	{
		Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
	}

	/**
	 * Show generic aler with message
	 * 
	 * @param message
	 */
	protected void showAlert(String message)
	{
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(message).setCancelable(false)
				.setPositiveButton("Ok", new DialogInterface.OnClickListener()
				{
					public void onClick(DialogInterface dialog, int id)
					{

					}
				});

		AlertDialog alert = builder.create();
		alert.show();
	}

	/**
	 * This abstract method is called in onCreate of AbstractScreen.java you
	 * should initialize all your variable in this method for the sake of clean
	 * code
	 */
	protected abstract void initUiVars();

	/**
	 * 
	 * @return Layout id of Screen this return value is used in onCreate of
	 *         AbstractScreen.java in setContentView(int layoutId) we are using
	 *         this strategy instead of directly assigning layout id via
	 *         R.layout.myId because in future we will be able to handle
	 *         different layouts for different devices easily
	 */
	protected abstract int getLayoutId();

	/**
	 * 
	 * @param to
	 *            Name of class to navigate
	 * 
	 *            <p>
	 *            Calling this method will switch screen to other activity
	 */
	protected void switchScreenTo(Class<?> to)
	{
		startActivity(new Intent(this, to));
	}

	/**
	 * 
	 * @param to
	 *            Name of class to navigate
	 * @param dataToCarry
	 *            A light weight data to transfer other activity will able to
	 *            receive that data currently you can transfer data of type
	 *            Integer, Long and String
	 */
	protected void switchScreenTo(Class<?> to, Object dataToCarry)
	{
		Intent intent = new Intent(this, to);
		if (dataToCarry instanceof Integer)
		{
			intent.putExtra("data", ((Integer) dataToCarry).intValue());
		}
		if (dataToCarry instanceof String)
		{
			intent.putExtra("data", (String) dataToCarry);
		} else if (dataToCarry instanceof Long)
		{
			intent.putExtra("data", (Long) dataToCarry);

		}
		startActivity(intent);
	}

	@Override
	protected void onDestroy()
	{

		super.onDestroy();
		onRelease();

	}

	@Override
	protected void onStart()
	{

		super.onStart();
	}

	@Override
	protected void onStop()
	{
		super.onStop();
	}

	/**
	 * if hideTitle is true default title bar will not be render
	 */
	private void hideDefalutTitle()
	{
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);

		// Remove notification bar
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
	}

	/**
	 * Any resources child class is using must release them on release will be
	 * call in onDestroy by AbstractScreen.Java
	 */
	protected abstract void onRelease();

	/**
	 * This method is called in onCreate of AbstractScreen.java all layout used
	 * in the application should be assign to LayoutIds.LayoutName for screen
	 * instead of directly assigning R.layout.layoutName we will use this
	 * variable
	 */
	private void initLayoutIds()
	{
		LayoutIds.SURVEY_LIST = R.layout.survey_list;
		LayoutIds.SURVEY_LIST_ROW = R.layout.survey_list_row;
		LayoutIds.DETAIL_SCREEN_ALL_CONTROLS = R.layout.survey_detail_all_controls;
		LayoutIds.DETAIL_SCREEN_SINGLE_CONTROL = R.layout.survey_detail_single_control;

		LayoutIds.SURVEY_IFNO_SCREEN = R.layout.survey_info;

		LayoutIds.SURVEY_OPTS_SCREEN = R.layout.survey_options;
		LayoutIds.SPLASH_SCREEN = R.layout.splash_mobile;

		LayoutIds.SURVEY_ANSWERS = R.layout.survey_answers;
		LayoutIds.GRAPH_SCREEN = R.layout.graph;

	}

	/**
	 * 
	 * @param message
	 *            Message to print in Console
	 */
	protected void printMessage(String message)
	{
		Log.i(this.getClass().getSimpleName(), message);
	}

	/**
	 * 
	 * @param message
	 *            Message to print on Android Log Cat
	 */
	protected void printLogMessage(String message)
	{
		Log.d(this.getClass().getName(), message);
	}

	/**
	 * class calling this methods must initialize 'protected headerImage field'
	 * 
	 * @param image
	 * @throws NullPointerException
	 *             if headerImage is not initialize
	 */
	protected void showHeaderImage(Image image) throws NullPointerException
	{
		if (null != image.getBitmap() && !image.getBitmap().isRecycled())
		{
			Drawable d = new BitmapDrawable(getResources(), image.getBitmap());

			int width = Device.getInstance().getPercentWidth(
					image.getPercentWidth() / 10f);
			int height = Device.getInstance().getPercentHeight(
					image.getPercentHeight() / 10f);

			this.headerImage.setLayoutParams(new LayoutParams(width, height));
			this.headerImage.setBackgroundDrawable(d);
			this.headerImage.setVisibility(View.VISIBLE);
		}
	}

}
